<template>
  <div class="app-container">
    <div class="filter-container" style="padding-bottom:20px;">
      <label class=" filter-item radio-label" style="padding-left:0;">主题: </label>
      <el-input v-model="fetchForm.topic" class="filter-item" style="width:250px;" />
      <label class=" filter-item radio-label" style="padding-left:0;">消息: </label>
      <el-input v-model="fetchForm.msg" class="filter-item" style="width:250px;" />
      <label class=" filter-item radio-label" style="padding-left:0;">日期: </label>
      <el-date-picker
        v-model="dateValue"
        class="filter-item"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"
        align="right"
        unlink-panels
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
        :default-time="['00:00:00', '23:59:59']"
        @change="dateChange"
      />
      <el-button class="filter-item" type="primary" style="float:right;" @click="fetchData()">查询</el-button>
    </div>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="10"
      @current-change="handleCurrentChange"
    />
    <el-table
      v-loading="logsLoading"
      :data="logs"
    >
      <el-table-column property="createdAt" label="日期" width="150" :formatter="dateFormat" />
      <el-table-column property="topic" label="主题" width="200" />
      <el-table-column property="msg" label="消息" />
    </el-table>
  </div>
</template>

<script>

import { emqxGetLogs } from '@/api/emqx'

export default {
  name: 'EmqxLog',
  components: { },
  props: { },
  data() {
    return {
      logs: [],
      logsLoading: false,
      total: 10,
      fetchForm: {
        topic: '',
        msg: '',
        start_time: '',
        end_time: '',
        page: 1,
        page_size: 10
      },
      dateValue: null,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
  },
  methods: {
    fetchData() {
      this.logsLoading = true
      console.log(this.fetchForm)
      emqxGetLogs(this.fetchForm).then(response => {
        this.logsLoading = false
        var data = response.data
        this.logs = data.data
        this.total = data.total
        console.log(data)
      })
    },
    handleCurrentChange(val) {
      this.fetchForm.page = val
      this.fetchData()
    },
    dateChange() {
      this.fetchForm.start_time = this.dateValue[0]
      this.fetchForm.end_time = this.dateValue[1]
    },
    dateFormat(row, column, cellValue, index) {
      const daterc = row[column.property]
      console.log(daterc)
      return this.$moment(daterc).format('yyyy-MM-DD HH:mm:ss')
    }

  }
}
</script>

<style lang="scss" scoped>

  .dashboard-editor-container {
    background-color: #e3e3e3;
    min-height: 100vh;
    padding: 50px 60px 0px;
  }

  .radio-label {
    font-size: 14px;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 0 30px;
  }
</style>
